{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.signal import savgol_filter\n",
    "\n",
    "# 定义四个文件夹路径\n",
    "folder_paths = [\n",
    "    r'D:\\20240405\\TestData-NR', \n",
    "    r'D:\\20240405\\TestData-1V', \n",
    "    r'D:\\20240406\\TestData-1-2V', \n",
    "    r'D:\\20240406\\TestData-2V'  # 新增文件夹路径\n",
    "]\n",
    "\n",
    "# 定义文件名模式\n",
    "file_pattern = re.compile(r'Test(\\d{2})#(2|5)\\.sts')\n",
    "\n",
    "# 初始化图形和轴对象\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "# 定义图例标签\n",
    "legend_labels = ['10mm阶段未加固', 'V形加固首层', 'V形加固上下两层', 'V形仅加固二层']\n",
    "\n",
    "# 定义每组文件夹对应的文件名列表\n",
    "file_name_pairs = [\n",
    "    ['Test10#2.sts', 'Test10#5.sts'],  # 文件夹1对应的文件名\n",
    "    ['Test10#2.sts', 'Test10#5.sts'],  # 文件夹2对应的文件名\n",
    "    ['Test9#2.sts', 'Test9#5.sts'],  # 文件夹3对应的文件名\n",
    "    ['Test10#2.sts', 'Test10#5.sts']   # 文件夹4对应的文件名\n",
    "]\n",
    "\n",
    "# 对每个文件夹进行操作\n",
    "for folder_index, folder_path in enumerate(folder_paths, start=1):\n",
    "    # 根据folder_index选择对应的文件名\n",
    "    file_names = file_name_pairs[folder_index-1]\n",
    "    \n",
    "    # 初始化X和Y数据数组\n",
    "    data_x, data_y = None, None\n",
    "    \n",
    "    # 遍历文件名，读取数据\n",
    "    for file_name in file_names:\n",
    "        file_path = os.path.join(folder_path, file_name)\n",
    "        match = file_pattern.match(file_name)\n",
    "        if match:\n",
    "            # 读取数据\n",
    "            file_data = np.fromfile(file_path, dtype=np.float32)\n",
    "            \n",
    "            # 根据文件后缀判断是X数组还是Y数组\n",
    "            if match.group(2) == '5':\n",
    "                data_x = file_data\n",
    "            elif match.group(2) == '2':\n",
    "                data_y = file_data\n",
    "        \n",
    "        # 当X和Y数组都准备好后，进行处理和绘制\n",
    "        if data_x is not None and data_y is not None:\n",
    "            # 确保数组长度相同\n",
    "            min_length = min(len(data_x), len(data_y))\n",
    "\n",
    "            # 对数据进行平滑处理\n",
    "            data_x_smooth = savgol_filter(data_x[:min_length], 101, 3) * 0.33\n",
    "            data_y_smooth = savgol_filter(data_y[:min_length], 101, 3) * 7\n",
    "\n",
    "            # 创建二维数组Circle并绘制\n",
    "            Circle = np.column_stack((data_x_smooth, data_y_smooth))\n",
    "            # 使用预定义的图例标签\n",
    "            ax.plot(Circle[:, 0], Circle[:, 1], label=legend_labels[folder_index-1])\n",
    "\n",
    "# 增加X=0轴和Y=0轴\n",
    "ax.axhline(0, color='black', linewidth=0.5)  # Y=0 轴线\n",
    "ax.axvline(0, color='black', linewidth=0.5)  # X=0 轴线\n",
    "\n",
    "# 设置图例\n",
    "ax.legend()\n",
    "\n",
    "# 显示图形\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
